package com.tongfusoft.document.web.controller.dictionary;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.Dictionary;
import com.tongfusoft.document.entity.DictionaryClass;
import com.tongfusoft.document.entity.SystemParam;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.dictionary.service.IDocumentDictionaryService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@Controller
public class DictionaryController extends BaseController{

	@Autowired
	@Qualifier("documentDictionaryService")
	public IDocumentDictionaryService documentDictService;

	@UrlDesc(value="字典表管理页")
	@RequestMapping("/dictionaryClass")
	public String dictPage(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "dictionaryClass");
		model.addAttribute("mainPage", "dictionary/dictionaryClass");

		return "base";
	}

	@UrlDesc(value="字典表分类查询")
	@RequestMapping("/dictionaryClass/query")
	@ResponseBody
	public DataTable<DictionaryClass> dictClassQuery(Model model, int length, int start, int draw, String extra_search,
			String order) {

		DataTable rm = new DataTable();
		try {
			DictionaryClass dictClass=(DictionaryClass)JsonUtils.getObject4JsonString(extra_search, DictionaryClass.class);

			int pageIndex = (start / length)+1;
			PageInfo pageInfo = this.documentDictService.dictCalssPageTable(pageIndex, length, dictClass);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {

			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="字典查询")
	@RequestMapping("/dictionary/query")
	@ResponseBody
	public DataTable<DictionaryClass> dictQuery(Model model, int length, int start, int draw, String dictClassId,
			String order) {

		DataTable rm = new DataTable();
		try {
			Dictionary dict = new Dictionary();
			dict.setDictclassId(dictClassId);
			int pageIndex = (start / length);
			PageInfo pageInfo = this.documentDictService.dictionaryPageTable(pageIndex, length, dict);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {

			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="字典分类新增")
	@RequestMapping("/dictionaryClass/add")
	@ResponseBody
	public ResultModel dictClassAdd(DictionaryClass dictClass) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.documentDictService.createDictionaryClass(dictClass, (Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}

	@UrlDesc(value="字典分类修改")
	@RequestMapping("/dictionaryClass/edit")
	@ResponseBody
	public ResultModel dictClassEidt(DictionaryClass dictClass) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.documentDictService.updateDictionaryClass(dictClass, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}

	@UrlDesc(value="字典分类删除")
	@RequestMapping("/dictionaryClass/del")
	@ResponseBody
	public ResultModel dictClassDel(DictionaryClass dictClass) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.documentDictService.deleteDictionaryClass(dictClass, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="字典分类详情")
	@RequestMapping("/dictionaryClass/detail")
	@ResponseBody
	public ResultModel dictClassDetail(DictionaryClass dictClass) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			rm.setData(this.documentDictService.getDictClass(dictClass));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="字典新增")
	@RequestMapping("/dictionary/add")
	@ResponseBody
	public ResultModel dictAdd(Dictionary dict){
		
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try{
			
			this.documentDictService.createDictionary(dict, (Users)super.getUser());
		}catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch(Exception e){
			rm.setState("error");
			e.printStackTrace();
		}
		
		
		
		return rm;
		
	}
	@UrlDesc(value="字典删除")
	@RequestMapping("/dictionary/del")
	@ResponseBody
	public ResultModel dictDel(Dictionary dict) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.documentDictService.deleteDictionary(dict, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	@UrlDesc(value="字典详情")
	@RequestMapping("/dictionary/detail")
	@ResponseBody
	public ResultModel dictClass(Dictionary dict) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			rm.setData(this.documentDictService.getDict(dict));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@UrlDesc(value="字典修改")
	@RequestMapping("/dictionary/edit")
	@ResponseBody
	public ResultModel dictEidt(Dictionary dict) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.documentDictService.updateDictionary(dict, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}

}
